package com.qf.day02;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 练习：秦灭六国，一统华夏。（模仿课堂案例，练习枚举类的使用）
 * @author lixu
 */
public class CountDownLatchDemo {

    //案例：6个同学陆续离开教室后---值班同学才可以关门。
    // 7个线程
    public static void main(String[] args) throws Exception{//主进程
        //闭锁
        CountDownLatch countDownLatch = new CountDownLatch(6);//计数器 初始值 6

        for (int i = 1; i < 7; i++) {
            String name = String.valueOf(i);
            new Thread(() -> {
                //逗留时间
                try {
                    TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("第" + name + "名同学离开了教室!!");
                countDownLatch.countDown();//减1
            }, name).start();
        }

        //阻塞
        countDownLatch.await();
        System.out.println("我是值班同学：我最后走!");


    }
}
